package com.seek.springevent.bean;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class DetailExecutorService extends ThreadPoolExecutor {

    private Logger logger = LoggerFactory.getLogger(DetailExecutorService.class);

    public DetailExecutorService(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,new ThreadPoolExecutor.CallerRunsPolicy());
    }

    public DetailExecutorService(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler rejectedExecutionHandler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,rejectedExecutionHandler);
    }

    @Override
    protected void beforeExecute(Thread t, Runnable r) {
        logger.info("线程数:{},队列大小:{},线程信息:{}",getActiveCount(),getQueue().size(), JSONObject.toJSON(r));
    }

    @Override
    protected void afterExecute(Runnable r, Throwable t) {
        logger.info("线程数:{},队列大小:{},线程信息:{},执行完成",getActiveCount(),getQueue().size(), JSONObject.toJSON(r));
        if (t!=null){
            logger.error("任务信息:{},执行错误：{}",JSONObject.toJSONString(r),t.getMessage());
        }
    }


}
